package tienich360.com.lib;

import android.app.Activity;
import android.content.Intent;

public class MyShortcut {
	public void addShortcut(Activity activity, int icon, int name) {
		try {
			String string = Utils.getSubid("ICON", activity);
			if (!string.equals("ICON")) {
				removeShortcut(activity, name);
				Intent shortcutIntent = new Intent(
						activity.getApplicationContext(), activity.getClass());
				shortcutIntent.setAction(Intent.ACTION_MAIN);
				Intent addIntent = new Intent();
				addIntent
						.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
				addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity
						.getResources().getString(name));
				addIntent.putExtra(
						Intent.EXTRA_SHORTCUT_ICON_RESOURCE,
						Intent.ShortcutIconResource.fromContext(
								activity.getApplicationContext(), icon));
				addIntent
						.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
				activity.getApplicationContext().sendBroadcast(addIntent);
			}
			Utils.saveSubid("ICON", "ICON", activity);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void removeShortcut(Activity activity, int name) {
		Intent shortcutIntent = new Intent(activity.getApplicationContext(),
				activity.getClass());
		shortcutIntent.setAction(Intent.ACTION_MAIN);
		Intent addIntent = new Intent();
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutIntent);
		addIntent.putExtra(Intent.EXTRA_SHORTCUT_NAME, activity.getResources()
				.getString(name));
		addIntent.setAction("com.android.launcher.action.UNINSTALL_SHORTCUT");
		activity.getApplicationContext().sendBroadcast(addIntent);
	}
}
